var dataJson = [];
$(function(){
	$("#addclothingType,#editclothingType").combobox({
		data:clothingType,
		valueField:'id',  
		textField:'text',
		editable:false
	})
	$("#clothType").combobox({
		data:clothingType,
		valueField:'id',  
		textField:'text',
	})
	loadData();
	$("#clothDatagrid").datagrid({
		data:dataJson,
	    fit:true,
	    toolbar:'#clothTolbar',
	    fitColumns:true,
	    rownumbers:true,
	    singleSelect:true,
	    columns:[[  
	        {field:'clothingId',hidden:true},
	        {field:'clothingPicUrl',hidden:true},
	        {field:'clothingName',title:'服装名称',width:100,align:"center"},
	        {field:'clothingBrand',title:'服装品牌',width:100,align:"center"}, 
	        {field:'clothingPrice',title:'服装价格',width:100,align:"center"}, 
	        {field:'clothingRent',title:'租金',width:100,align:"center"}, 
	        {field:'stock',title:'库存',width:100,align:"center"}, 
	        {field:'creatTime',hidden:true,title:'服装名称',width:100,align:"center"}, 
	        {field:'updateTime',hidden:true,title:'服装名称',width:100,align:"center"}, 
	        {field:'clothingType',title:'服装类别',width:40,align:"center",
	        	formatter:function(value,row,index){
	        		return typeToName(value,clothingType);
	        	}	
	        },  
	        {field:'operation',title:'操作',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return " <a  href='#' onclick=editRow("+index+");  >编辑</a> "+
	                  " <a  href='#' onclick=deleteRow('"+row.clothingId+"'); >删除</a> ";
	        	}
	        	
	        },
	    ]]  
	
	});
	
	$("#addDialog").dialog({
		title: '新增服装信息',  
	    width: 310,  
	    height:330,  
	    closed: true,  
	    cache: false,    
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Clothing/addClothing.json",
					type:"post",
					data:JSON.stringify({
						"clothingId":$("#addclothingId").val(),
						"clothingName":$("#addclothingName").val(),
						"clothingBrand":$("#addclothingBrand").val(),
						"clothingType":$("#addclothingType").combobox("getValue"),
						"clothingPrice":$("#addclothingPrice").val(),
						"clothingRent":$("#addclothingRent").val(),
						"stock":$("#addstock").val()
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "新增成功！",'info'); 
						}else{
							$.messager.alert("信息", "新增失败！",'info'); 
						}
						loadData();
						$("#clothDatagrid").datagrid("loadData",dataJson);
						$("#addDialog").dialog("close");
					}
				})
			}
		},{
			text:'关闭',
			handler:function(){
				$("#addDialog").dialog("close");
			}
		}]
	});
	
	$("#editDialog").dialog({
		title: '编辑服装信息',  
	    width: 310,  
	    height:330,  
	    closed: true,  
	    modal: true ,
	    buttons:[{
			text:'保存',
			handler:function(){
				$.ajax({
					url:$WEB_ROOT_PATH+"/Clothing/editClothing.json",
					type:"post",
					data:JSON.stringify({
						"clothingId":$("#editclothingId").val(),
						"clothingName":$("#editclothingName").val(),
						"clothingBrand":$("#editclothingBrand").val(),
						"clothingType":$("#editclothingType").combobox("getValue"),
						"clothingPrice":$("#editclothingPrice").val(),
						"clothingRent":$("#editclothingRent").val(),
						"stock":$("#editstock").val()
					}),
					contentType: "application/json",
					dataType:"json",
					success:function(result){
						if(result.data.success == "0"){
							$.messager.alert("信息", "修改成功！",'info'); 
						}else{
							$.messager.alert("信息", "修改失败！",'info'); 
						}
						loadData();
						$("#clothDatagrid").datagrid("loadData",dataJson);
						$("#editDialog").dialog("close");
					}
				})
				
			}
		},{
			text:'关闭',
			handler:function(){
				$("#editDialog").dialog("close");
			}
		}]
	})
})

function loadData(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Clothing/getClothingList.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"clothingName":'',"clothingType":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.clothingInfo;
		}
	})
}
function doAdd(){
	$("#addDialog").dialog("center");
	$("#addDialog").dialog("open");
	$("#addDialogForm").form("clear");
}

function editRow(index){
	$("#editDialog").dialog("center");
	$("#editDialog").dialog("open");

	$('#clothDatagrid').datagrid("selectRow",index);
	var row = $('#clothDatagrid').datagrid("getSelected");
	if(!row){
		$('#clothDatagrid').datagrid("selectRow",index);
		row = $('#clothDatagrid').datagrid("getSelected");
	}
	$("#editclothingId").val(row.clothingId);
	$("#editclothingName").val(row.clothingName);
	$("#editclothingBrand").val(row.clothingBrand);
	$("#editclothingType").combobox("setValue",row.clothingType);
	$("#editclothingPrice").val(row.clothingPrice);
	$("#editclothingRent").val(row.clothingRent);
	$("#editstock").val(row.stock);
	
}

function deleteRow(id){
	 $.messager.confirm("操作提示", "您确定要删除吗？", function (data) {  
         if (data) {  
			$.ajax({
				url:$WEB_ROOT_PATH+"/Clothing/deleteClothing.json",
				type:"post",
				async: false, 
				data: JSON.stringify({"clothingId":id}),
				contentType: "application/json",
				dataType:"json",
				success:function(result){
					if(result.data.success == "0"){
						$.messager.alert("信息", "删除成功！",'info'); 
					}else{
						$.messager.alert("信息", "删除失败！",'info'); 
					}
					loadData();
					$("#clothDatagrid").datagrid("loadData",dataJson);
				}
			})
         } else {  
             return;  
          }  
      }); 
}

function doSearch(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Clothing/getClothingList.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"clothingName":$("#clothName").val(),"clothingType":$("#clothType").combobox("getValue")}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.clothingInfo;
			$("#clothDatagrid").datagrid("loadData",dataJson);
		}
	})
}
